package com.iss.ljx.user.service.impl;

import com.iss.ljx.login.entity.LoginUserEntity;
import com.iss.ljx.login.repository.LoginDao;
import com.iss.ljx.mybatis.entity.PtUserEntity;
import com.iss.ljx.mybatis.flexigrid.PadingRstType;
import com.iss.ljx.mybatis.flexigrid.PagingBean;
import com.iss.ljx.mybatis.ztree.ZtreeEntity;
import com.iss.ljx.user.entity.SearchUserEntity;
import com.iss.ljx.user.repository.UserDao;
import com.iss.ljx.user.repository.UserInfoDao;
import com.iss.ljx.user.service.UserService;
import com.iss.ljx.util.DataUtil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private UserDao userDao;
    @Resource
    private LoginDao loginDao;
    @Override
    public List<PtUserEntity> queryUserListByName(SearchUserEntity search) {
        return userInfoDao.queryUserInfoList(search);
    }

    @Override
    public boolean deleteUserInfoById(String ids) {
        //查询登录者的基本信息
        LoginUserEntity userDetails = (LoginUserEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userUUID = userDetails.getUserUuid();
        //查询id对应的用户信息
        PtUserEntity ptUser = userDao.queryUserById(userUUID);

        String[] idArray = ids.split(",");
        for (String id : idArray) {
            String did = userDao.queryUserById(id).getDepid();
            if ("".equals(ptUser.getDepid())) {
                userInfoDao.deleteUserInfoById(id);
            } else if (ptUser.getDepid().equals(did)) {
                userInfoDao.deleteUserInfoById(id);
            } else {
                return false;
            }
        }
        return true;
    }

    @Override
    public PadingRstType<PtUserEntity> getUserInfolistByPage(SearchUserEntity search, PagingBean paging) {
        paging.deal(PtUserEntity.class);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = new PadingRstType<>();
        //查询登录者的基本信息
        LoginUserEntity userDetails = (LoginUserEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userUUID = userDetails.getUserUuid();
        PtUserEntity ptUser1 = loginDao.queryUserInfoByUserUUid(userUUID);


        List<PtUserEntity> list=null;
        //判断是否为系统管理员
        if ("".equals(ptUser1.getDepid())){
            list = userInfoDao.getUserInfolistByPage(search, paging);
        }else{
            //查询部门名称对应的id
            String id=userInfoDao.getUserDepId(search.getDepName());
            if (id.equals(ptUser1.getDepid())){
                list = userInfoDao.getUserInfolistByPage(search, paging);
            }
        }
        Integer total = userInfoDao.getUserInfolistTotal(search);
        ptUserEntityPadingRstType.setPage(paging.getPage());
        ptUserEntityPadingRstType.setRawRecords(list);
        ptUserEntityPadingRstType.setTotal(total);
        ptUserEntityPadingRstType.putItems();
        return ptUserEntityPadingRstType;
    }

    @Override
    public List<ZtreeEntity> getRoleZtreeListByUserId(String userUuid) {
        List<ZtreeEntity> ztreeEntityList = userInfoDao.queryRoleZtreeListAll();
        List<String> selectRoleIdList = userInfoDao.queryRoleZtreeListByUserId(userUuid);
        for (ZtreeEntity roleEntity : ztreeEntityList) {
            for (String roleId : selectRoleIdList) {
                if (roleId.equals(roleEntity.getId())) {
                    roleEntity.setChecked(true);
                    break;
                }
            }
        }

        return ztreeEntityList;
    }

    @Override
    public void allocateRoleByUser(String userUuid, String roleArray) {
        userInfoDao.delRoleByUserId(userUuid);
        String[] roles = roleArray.split(",");
        for (String roleId : roles) {
            userInfoDao.insertRoleRefUser(roleId, userUuid);
        }
    }

    @Override
    public void insertUser(PtUserEntity ptUser) {
        String uuid = DataUtil.getUUIDShort();
        ptUser.setUserUuid(uuid);

        //查询登录者的基本信息
        LoginUserEntity userDetails = (LoginUserEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userUUID = userDetails.getUserUuid();
        PtUserEntity ptUser1 = loginDao.queryUserInfoByUserUUid(userUUID);
        //判断登录人是否为部门管理员
        if (!"".equals(ptUser1.getDepid())) {
            ptUser.setDepid(ptUser1.getDepid());
        }
        ptUser.setRegisterdate(new Date());
        ptUser.setDelFlag(0);
        ptUser.setModtime(new Date());

        userDao.insertUser(ptUser);
    }

    @Override
    public boolean updateUser(PtUserEntity ptUser) {

        //查询登录者的基本信息
        LoginUserEntity userDetails = (LoginUserEntity) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userUUID = userDetails.getUserUuid();
        PtUserEntity ptUser1 = loginDao.queryUserInfoByUserUUid(userUUID);
        //判断登录人是否为系统管理员
        if ("".equals(ptUser1.getDepid()) || ptUser.getDepid().equals(ptUser1.getDepid())) {
            ptUser.setModtime(new Date());
            ptUser.setModifierid(ptUser.getUserUuid());
            userDao.updateUser(ptUser);
            return true;
        }else{
            return false;
        }
    }
}
